from collections.abc import Iterable


def fn(x):
    return x * x


def fn1(x, y):
    return x + y


def fn2(n):
    return n % 2 == 1


def map(fn, lst: list):
    if not isinstance(lst, Iterable):
        print("Iterable is not Iterable")
        return []
    result = []
    for i in lst:
        result.append(fn(i))
    return result


def reduce(fn, lst: list):
    if not isinstance(lst, Iterable):
        print("Iterable is not Iterable")
        return
    result = 0
    for i in lst:
        result = fn(result, i)
    return result


def filter(fn, lst: list):
    if not isinstance(lst, Iterable):
        print("Iterable is not Iterable")
        return
    result = []
    for i in lst:
        if fn2(i):
            result.append(i)
    return result


def main():
    # map函数
    ret = list(map(fn, 1))
    print(ret)
    ret = list(map(fn, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
    print(ret)
    ret = list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
    print(ret)

    # reduce函数
    ret = reduce(fn1, [1, 3, 5, 7, 9])
    print(ret)

    # filter函数
    ret = list(filter(fn2, [1, 2, 4, 5, 6, 9, 10, 15]))
    print(ret)

if __name__ == '__main__':
    main()
    # 通过这个有没有认识了什么吗？
